package com.yujn.order.controller;

import com.yujn.common.bean.Result;
import com.yujn.order.bean.DynamicPerson;
import com.yujn.order.bean.DynamicPerson2;
import com.yujn.order.feign.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RequestMapping("/test")
@RestController
public class TestController {

    @Autowired
    private ProductFeign productFeign;

    @GetMapping("/test")
    public Result test(HttpServletRequest request){
        return productFeign.getProductById("1111");
    }

    @Autowired
    private Environment environment;

    /**
     * 动态获取配置中心的 配置值，用于测试在配置中心后台修改配置数据，能否动态刷新
     * @return
     */
    @GetMapping("/getDynamicProperty")
    public Result getDynamicProperty(){
        String name = environment.getProperty("dynamic.person.name");
        String sex = environment.getProperty("dynamic.person.sex");
        Integer age = environment.getProperty("dynamic.person.age", Integer.class);

        HashMap<String, Object> data = new HashMap<>();
        data.put("name",name);
        data.put("sex",sex);
        data.put("age",age);

        return Result.success(data);
    }

    @Autowired
    private DynamicPerson dynamicPerson;


    /**
     * 单利BEAN 一开始初始化，虽然内存配置值已经变化，但bean还是旧bean
     * 通过@ScopRefresh刷新
     *
     * 测试发现，会自动更新，难道通过ConfigurationProperties方式配置的，不是缓存的单利bean
     * @return
     */
    @GetMapping("/getDynamicPerson")
    public Result getDynamicPerson(){
        return Result.success(dynamicPerson);
    }


    @Autowired
    private DynamicPerson2 dynamicPerson2;


    /**
     * 单利BEAN 一开始初始化，虽然内存配置值已经变化，但bean还是旧bean
     * 通过@ScopRefresh刷新
     *
     * 通过@Value注入的值，确实不会动态刷新
     * @return
     */
    @GetMapping("/getDynamicPerson2")
    public Result getDynamicPerson2(){
        return Result.success(dynamicPerson2);
    }

}
